package com.bibrApp.difu.actvity.recharge_b_mvp;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bibrApp.difu.DfApp;
import com.bibrApp.difu.R;
import com.bibrApp.difu.Response.RechargeAddressData;
import com.bibrApp.difu.actvity.record_mvp.RecordActivity;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.utils.IntentUtil;
import com.bibrApp.difu.utils.SaveBitmapUtil;
import com.bibrApp.difu.utils.SystemClipboardUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.uuzuche.lib_zxing.activity.CodeUtils;


import butterknife.BindView;
import butterknife.OnClick;

/**
 * 充币
 */
public class RechargeBActivity extends BaseActivity<RechargeBPresenter> implements IRechargeBView {

    @BindView(R.id.recharge_b_title_tv)
    TextView titleTV;
    @BindView(R.id.recharge_b_address_tv)
    TextView addressTV;
    @BindView(R.id.small_recharge_tv)
    TextView smallTV;
    @BindView(R.id.recharge_b_remark_tv)
    TextView remarkeTV;
    @BindView(R.id.recharge_b_sava_zxing_btn)
    Button saveZxingBtn;
    @BindView(R.id.recharge_b_copy_address_btn)
    Button copyAddressBtn;
    @BindView(R.id.recharge_b_content_ll)
    LinearLayout contentLL;

    @BindView(R.id.recharge_b_zxing_iv)
    ImageView zxingIV;

    private Context mContext;
    private ImmersionBar mImmersionBar;
    public static final String ACCOUNT_TYPE = "account_type";
    private Bitmap mBitmap;
    private DfApp dfApp;

    @Override
    protected void initPresenter() {
        presenter = new RechargeBPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recharge_b;
    }

    @Override
    public void error(String msg) {
        contentLL.setVisibility(View.GONE);
        ToastUtils.ShowToast(mContext, msg);
    }



    @Override
    public void success(RechargeAddressData data) {
        if (!TextUtils.isEmpty(data.getWalletAddress())) {
            contentLL.setVisibility(View.VISIBLE);
            addressTV.setText(data.getWalletAddress());
            mBitmap = CodeUtils.createImage(data.getWalletAddress(), 400, 400, null);
            zxingIV.setImageBitmap(mBitmap);
        } else {
            contentLL.setVisibility(View.GONE);
        }

//        smallTV.setText("最小充币数量为0.0001");
        if(data.getRcAddressRemark() != null && !TextUtils.isEmpty(data.getRcAddressRemark())){
            smallTV.setVisibility(View.VISIBLE);
            smallTV.setText(data.getRcAddressRemark());
        }else{
            smallTV.setVisibility(View.GONE);
        }
        remarkeTV.setText(data.getRemark());

    }



    @Override
    public void errorOfLogin(String msg) {

    }


    @Override
    public void initView() {
        mContext = this;
        dfApp = (DfApp) getApplication();
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.barColor(R.color.white)
                .statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .init();

        String typeName = getIntent().getStringExtra("name");
        if (!TextUtils.isEmpty(typeName)) {
            titleTV.setText(typeName);
        }
       presenter.getRechargeAddressP(typeName,dfApp.getUserId());

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }
    }


    @OnClick(R.id.recharge_financial_record)
    public void onClickFinancialRec() {
        IntentUtil.toActivity(RecordActivity.class, this);
    }



    @OnClick(R.id.recharge_b_copy_address_btn)
    public void onClickCopyAddress() {
        if (!TextUtils.isEmpty(addressTV.getText().toString())) {
            ToastUtils.ShowToast(mContext, "复制成功");
            SystemClipboardUtils.copyToClipboard(addressTV.getText().toString(), mContext);
        } else {
            ToastUtils.ShowToast(mContext, "地址不存在");
        }

    }


    @OnClick(R.id.recharge_b_sava_zxing_btn)
    public void onClickSaveZxingBtn() {
        String picName = titleTV.getText().toString() + "地址二维码";
        if (mBitmap != null) {
            SaveBitmapUtil.saveBmp2Gallery(mContext, mBitmap, picName);
        }
    }

    @OnClick(R.id.return_iv)
    public void onClickBack() {
        finish();
    }

}
